package cn.e2win.biz.sms;

import java.sql.SQLException;

import org.springframework.stereotype.Repository;

import cn.e2win.base.json.vo.Pager;
import cn.e2win.biz.base.BaseDAOHibernateImpl;
import cn.e2win.biz.util.DateUtils;
import cn.e2win.biz.util.StringUtils;


@Repository
public class MsgDaoImpl extends BaseDAOHibernateImpl implements MsgDao {
	
	@Override
	public void pagerMsgLogs(Pager pager) {
		String sql = "SELECT t.* FROM msg_logs t WHERE 1=1 ORDER BY t.id DESC";
		
		try {
			pager.processSql(sql);
			processPager(pager);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void pagerCust4SendMsg(Pager pager) {
		Object orderBy = pager.getScMap().get("orderBy_I");
		if(StringUtils.isEmpty(orderBy)){
			orderBy = "amt";
		}
		String bDate = (String)pager.getScMap().get("createDate_B_I");
		String eDate = (String)pager.getScMap().get("createDate_E_I");
		
		String sql = " SELECT  t1.*, t0.custName, t0.cellPhone " +
				" FROM crm_customer t0  LEFT JOIN (" +
				"     SELECT t1.custId,MAX(t1.createDate) lastBizDate , SUM(t1.pfms) AS amt, SUM(t1.unPay) AS unPay" +
				"     FROM order_ t1 " +
				"     WHERE t1.activeStatusInt=1 " +
				(StringUtils.isEmpty(bDate)?"" : " AND createDate>='"+bDate + "' ") + 
				(StringUtils.isEmpty(eDate)?"" : " AND createDate<='"+DateUtils.date2Str(DateUtils.getOffsetDay(DateUtils.str2Date(eDate), 1)) + "' ") + 
				"     GROUP BY t1.custId " +
				" ) t1  ON t0.id = t1.custId" +
				" WHERE 1=1 " +
				" AND cellPhone IS NOT NULL " +
				" ORDER BY " + orderBy + " DESC";
		try {
			pager.processSql(sql);
			processPager(pager);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}	 
}
